package map.android.baidu.carpool.utils;

import android.app.Activity;
import android.app.Dialog;
import android.graphics.drawable.ColorDrawable;
import android.os.Handler;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.animation.Animation;
import android.widget.TextView;
import com.baidu.components.api.extra.APIProxy;
import com.baidu.lbs.net.http.model.ComNetData;
import com.baidu.lbs.net.http.model.IDataStatusChangedListener;
import com.baidu.lbs.net.http.response.ComNetResponse;
import com.baidu.mapframework.component.comcore.exception.ComException;
import com.baidu.mapframework.component.comcore.impl.message.DefaultComRequestFactory;
import com.baidu.mapframework.component.comcore.impl.message.params.ComBaseParams;
import com.baidu.mapframework.component.comcore.manager.ComponentManager;
import com.baidu.mapframework.component.comcore.message.ComParams;
import com.baidu.mapframework.component.comcore.message.ComRequest;
import map.android.baidu.carpool.Config;
import map.android.baidu.carpool.R;
import map.android.baidu.carpool.animation.AnimationListenerForCarpool;
import map.android.baidu.carpool.animation.AnimationUtilsForCarpool;
import map.android.baidu.carpool.data.CarpoolRequestParam;
import map.android.baidu.carpool.data.impl.ReceiveDaLiBaoData;
import map.android.baidu.carpool.response.CreateCommentResponse;

import java.util.HashMap;

/*
 * 订单详情页分享红包到朋友圈和微信好友
 */
public class OrderHongbaoShareDialog extends Dialog implements View.OnClickListener {
	public static final String TAG = "CarOrderHongbaoShareDialog";

	private TextView mWXhaoyouTextView;
	private TextView mWXpengyouquanTextView;
	private TextView mCancelTextView;
	private CreateCommentResponse createCommentResponse;
	private View mView;
    private Activity mActivity;

	public OrderHongbaoShareDialog(Activity activity, CreateCommentResponse createCommentResponse) {
		super(activity, 0);
		if (activity == null) {
			return;
		}
		this.createCommentResponse = createCommentResponse;
		initUI(activity);
	}

	private void initUI(Activity activity) {
	    mView = APIProxy.res().inflate(R.layout.hotel_page_order_detail_hongbao_dialog, null);
	    AnimationUtilsForCarpool.getInstance().translateBottomToTop(mView);
	    requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(mView);
		setCancelable(false); //不可以使用返回键和空白区消除dialog
		mWXhaoyouTextView = (TextView) mView.findViewById(R.id.hotel_order_detail_hongbao_weixinhaoyou);
		mWXpengyouquanTextView = (TextView) mView.findViewById(R.id.hotel_order_detail_hongbao_weixinpengyouquan);
		mCancelTextView = (TextView) mView.findViewById(R.id.hotel_order_detail_hongbao_cancel_btn);

		mWXhaoyouTextView.setOnClickListener(this);
		mWXpengyouquanTextView.setOnClickListener(this);
		mCancelTextView.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
        if(createCommentResponse == null){
            return;
        }
        if(createCommentResponse.data == null){
            return;
        }
        if(createCommentResponse.data.shareInfo == null){
            return;
        }
		switch (v.getId()) {
		case R.id.hotel_order_detail_hongbao_weixinhaoyou:// 微信好友
            if(Config.MONKEY_OPEN){
                return;
            }
			DefaultComRequestFactory factory = new DefaultComRequestFactory();
			ComRequest request = factory.newComRequest("mainmap", ComRequest.METHOD_DISPATCH);
			ComParams params = new ComBaseParams();
			params.setTargetParameter("social_share");
			HashMap<String, Object> map = new HashMap<String, Object>();
			map.put("weixin_send_url", true);
			map.put("weixin_timeline_title", createCommentResponse.data.shareInfo.edit);// 分享的标题
			map.put("title", createCommentResponse.data.shareInfo.edit);// 分享的标题
			map.put("content", createCommentResponse.data.shareInfo.detail);// 分享的内容
			map.put("weixin_description", createCommentResponse.data.shareInfo.detail);// 分享的内容
			map.put("url", createCommentResponse.data.shareInfo.url + CarpoolRequestParam.noNeedPostParams());// 分享的链接
			map.put("thumb_img_url", createCommentResponse.data.shareInfo.imgUrl);// 分享的图片
			map.put("share_type", new Integer(2));
			params.setBaseParameters(map);
			request.setParams(params);
			try {
                ComponentManager.getComponentManager().dispatch(request);
                if(createCommentResponse.data.shareInfo.isSpecial == 1){
                    sendDaLiBao(createCommentResponse);
                }
			} catch (ComException e) {
			}
			dismissDialogDoDone();
			break;
		case R.id.hotel_order_detail_hongbao_weixinpengyouquan:// 微信朋友圈
            if(Config.MONKEY_OPEN){
                return;
            }
			DefaultComRequestFactory factory2 = new DefaultComRequestFactory();
			ComRequest request2 = factory2.newComRequest("mainmap", ComRequest.METHOD_DISPATCH);
			ComParams params2 = new ComBaseParams();
			params2.setTargetParameter("social_share");
			HashMap<String, Object> map2 = new HashMap<String, Object>();
			map2.put("weixin_send_url", true);
            map2.put("weixin_timeline_title", createCommentResponse.data.shareInfo.edit);// 分享的标题
            map2.put("title", createCommentResponse.data.shareInfo.edit);// 分享的标题
            map2.put("content", createCommentResponse.data.shareInfo.detail);// 分享的内容
            map2.put("weixin_description", createCommentResponse.data.shareInfo.detail);// 分享的内容
            map2.put("url", createCommentResponse.data.shareInfo.url + CarpoolRequestParam.noNeedPostParams());// 分享的链接
            map2.put("thumb_img_url", createCommentResponse.data.shareInfo.imgUrl);// 分享的图片
            map2.put("img_url", createCommentResponse.data.shareInfo.imgUrl);
			map2.put("share_type", new Integer(3));
			params2.setBaseParameters(map2);
			request2.setParams(params2);
			try {
				ComponentManager.getComponentManager().dispatch(request2);
                if(createCommentResponse.data.shareInfo.isSpecial == 1){
                    sendDaLiBao(createCommentResponse);
                }
			} catch (ComException e) {
			}
			dismissDialogDoDone();
			break;
		case R.id.hotel_order_detail_hongbao_cancel_btn:
		    dismissDialogDoDone();
			break;
		}
	}

    private IDataStatusChangedListener listener = new IDataStatusChangedListener() {
        @Override
        public void onDataStatusChanged(ComNetData data, ComNetResponse response, int dataStatus) {

        }
    };
    private void sendDaLiBao(CreateCommentResponse createCommentResponse) {

        final ReceiveDaLiBaoData daLiBaoData = new ReceiveDaLiBaoData(mActivity);
        daLiBaoData.setActId(createCommentResponse.data.shareInfo.actId);
        daLiBaoData.setCode(createCommentResponse.data.shareInfo.code);
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                daLiBaoData.post(listener);
            }
        }, 10000);
    }

    private void dismissDialogDoDone(){
        AnimationUtilsForCarpool.getInstance().translateBottomToTopReverse(mView,new AnimationListenerForCarpool() {
            @Override
            public void onAnimationEnd(Animation animation) {
                dismiss();
            }
        });
    }
	
	@Override
    public void show() {
        super.show();
        ColorDrawable dw = new ColorDrawable(0x0000ff00);
        getWindow().setBackgroundDrawable(dw);
        getWindow().setGravity(Gravity.BOTTOM);
        int width = APIProxy.sys().getScreenWidth();
        getWindow().setLayout(width, -2);
    }
}